<?php
namespace app\wechat\controller;


class RMember extends Common
{
    public function index()
    {
        /*PJ非微信本地测试方法*/
        if( @intval($_COOKIE["PJ"]) ){session('member_id',@intval($_COOKIE["PJ"]));}

        $member_id = session('member_id');
        // 检查是否有维修店权限
        $repair = db('repair')->where('member_id', $member_id)->find();
        $this->assign('repair', $repair);
        if (!$repair) {
            // 没有申请 转入申请提示页面
            return view('r_member/audit');
        } else if ($repair['audit_status'] != 4) {
            // 审核状态页面
            $this->redirect('r_certification/audit_status', ['status' => $repair['audit_status']]);

        }

        /*
        $map['m.openid'] = session('openid');
        $map['m.band_status'] = 1;
        $member_info = db('member')->alias('m')->field('m.*, mr.signature, mr.nickname, mr.headimgurl')
            ->join("mob_member_role_info mr", "m.id = mr.member_id and mr.type = 1", 'LEFT')->where($map)->find();
        
        // 如果头像和昵称不存在则使用微信头像和昵称
        $member_info['nickname'] ? $member_info['nickname'] : $member_info['nickname'] = session('nickname');
        $member_info['headimgurl'] ? $member_info['headimgurl'] : $member_info['headimgurl'] = session('headimgurl');

        $this->assign('member', $member_info);

        */

        // 获取维修店首页数据
        $param = [
            'member_id' => session('member_id'),
            'terrace' => 'weixin'
        ];
        $data = call_interface('repair_index_data',$param);
        if($data["code"]==200){
            $this->assign('data', $data["data"]);
        }else{
            return $data["msg"];
        }

        return view();
    }

    public function index_map()
    {

        return view();
    }

    // 获取订单列表
    public function get_orders() {

        if (request()->isAjax()) {
            // 获取订单列表接口
            $order_id = intval(input('param.order_id',0));
            $repair_id = intval(input('param.repair_id',0));
            $param = [
                'member_id' => session('member_id')
            ];
            $data = call_interface('repair_get_orders',$param);
            return $data;
        }
        $this->redirect('base/error');

    }

    // 获取订单列表(地图范围内)
    public function get_orders_map() {

        if (request()->isAjax()) {
            // 获取订单列表接口(地图范围内)
            //$get_type = intval(input('param.type',0));
            $get_lng = floatval(input('param.lng',0));
            $get_lat = floatval(input('param.lat',0));
            $get_zoom = floatval(input('param.zoom',0));
            $param = [
                'member_id' => session('member_id')
                ,'lng' => $get_lng
                ,'lat' => $get_lat
                ,'zoom' => $get_zoom
            ];
            $data = call_interface('repair_get_orders_map',$param);
            return $data;
        }
        $this->redirect('base/error');

    }

    // 订单信息
    public function order_info() {

        /*PJ非微信本地测试方法*/
        if( @intval($_COOKIE["PJ"]) ){session('member_id',@intval($_COOKIE["PJ"]));}

        $map['member_id'] = session('member_id');
        $repair = db('repair')->field('id,repair_name,lat,lon')->where($map)->find();
        if(!$repair){
            $repair = [
                'id' => 0,
                'lat' => 0,
                'lon' => 0
            ];
        }
        $this->assign('repair', $repair);

        $id = intval(input('param.id',0));
        $order_info = db('order_server')->alias('o')->field('o.*, mr.headimgurl')
            ->join("mob_member_role_info mr", "o.member_id = mr.member_id and mr.type = 0", 'LEFT')
            ->where("o.id",$id)->find();
        $this->assign('order', $order_info);

        return view();
    }

    // 接订单
    public function order_get() {


        if (request()->isAjax()) {

            $order_id = intval(input('param.order_id',0));
            $repair_id = intval(input('param.repair_id',0));

            $order_info = db('order_server')->where("id",$order_id)->find();
            if($order_info["status"]==1){
                // 维修店抢单接口
                $param = [
                    'order_id' => $order_id,
                    'repair_id' => $repair_id
                ];
                $data = call_interface('order_server_receiving',$param);
                return $data;
            }else{
                if($repair_id==$order_info['order_info']){
                    $tmpStr = "您已经接受了这个订单";
                }else{
                    $tmpStr = "已经被别的维修店接受";
                }
                return ['data'=>$order_info["status"], 'code'=>0, 'msg'=> $tmpStr.'('.get_server_order_status($order_info["status"]).')' ];
            }

        }
        $this->redirect('base/error');

    }

    // 实名认证
    public function certification() {
        return view();
    }
}
